#!/bin/bash

if [ -z $1 ]
then
	echo " location can not be null"
	exit 1
else	
	PROJECT_DIR=$1
fi

MODULE_NAME=$2

# 查找脚本自身所在目录
SOURCE="$0"
while [ -h "$SOURCE"  ]; do # resolve $SOURCE until the file is no longer a symlink
	DIR="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"
	SOURCE="$(readlink "$SOURCE")"
	[[ $SOURCE != /*  ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"

echo $DIR 
echo $PROJECT_DIR

function createService(){
       serviceFileName=$1
       entity=$2
  #        创建service
        echo "package org.dtrd.modules.${MODULE_NAME}.service;" >> $serviceFileName
        echo ''  >> $serviceFileName
        echo "import com.baomidou.mybatisplus.extension.service.IService;"  >> $serviceFileName
        entity=${mapper//Mapper/''}
        echo "import org.dtrd.modules.${MODULE_NAME}.entity.po.${entity};"  >> $serviceFileName
        echo ''  >> $serviceFileName
        echo '/**'  >> $serviceFileName
        echo '@author 萧'  >> $serviceFileName
        time3=$(date "+%Y-%m-%d %H:%M:%S")
        echo "@date ${time3}"  >> $serviceFileName
        echo "**/"  >> $serviceFileName
        echo "public interface $serviceFileName extends IService<${entity}> {"  >> $serviceFileName
        echo ""  >> $serviceFileName
        echo "}"  >> $serviceFileName
        sed -i "" "s/.java//g" $serviceFileName
}

function createServiceImpl() {
        serviceImpl=$1
        entity=$2
        mapper=$3
        serviceFileName=$4
        echo "serviceFileName=$serviceFileName serviceImpl=$serviceImpl entity=$entity mapper=$mapper"
        echo "package org.dtrd.modules.${MODULE_NAME}.service.impl;" >> $serviceImpl
        echo ''  >> $serviceImpl
        echo "import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;" >> $serviceImpl
        echo "import lombok.extern.slf4j.Slf4j;" >> $serviceImpl
        echo "import org.dtrd.modules.${MODULE_NAME}.entity.po.${entity};"  >> $serviceImpl
        echo "import org.dtrd.modules.${MODULE_NAME}.mapper.$mapper;"  >> $serviceImpl
        echo "import org.dtrd.modules.${MODULE_NAME}.service.$serviceFileName;"  >> $serviceImpl
        echo "import org.springframework.stereotype.Service;"  >> $serviceImpl
        echo '/**'  >> $serviceImpl
        echo '@author 萧'  >> $serviceImpl
        time3=$(date "+%Y-%m-%d %H:%M:%S")
        echo "@date ${time3}"  >> $serviceImpl
        echo "**/"  >> $serviceImpl
        echo "@Service
@DS(\"multi-datasource1\")" >> $serviceImpl
        echo "@Slf4j" >> $serviceImpl
        echo "public class $serviceImpl extends ServiceImpl<$mapper, $entity> implements $serviceFileName {"  >> $serviceImpl
        echo ""  >> $serviceImpl
        echo "}"  >> $serviceImpl
        sed -i "" "s/.java//g" $serviceImpl
}

function read_dir(){
    cd $PROJECT_DIR
    for file in `find . | grep -E 'Mapper.java'`       #注意此处这是两个反引号，表示运行系统命令
    do
        suffix=${file##*.}
        echo "suffix = $suffix  file-=$file"
        mapper="${file##*/}"
        serviceName="${mapper//Mapper/Service}"
        pwd
        cd ../service
        serviceFileName="I$serviceName"
        if [ -e $serviceFileName ];then
          echo "$serviceFileName exist "
        else
          touch "$serviceFileName"
          createService $serviceFileName $entity
        fi
        path=`pwd`

        # 创建serviceImpl
        cd impl
        serviceImpl="${serviceName//.java/Impl.java}"

        if [ -e $serviceImpl ];then
          echo "$serviceImpl exist "
        else
          touch "$serviceImpl"
          createServiceImpl $serviceImpl $entity $mapper $serviceFileName
        fi

        cd ..


    done
}



read_dir